package codeRandomThoughts.Test122买卖股票的最佳时机II;

public class Solution2 {

    /**
     * 贪心算法
     *
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        //单独求出每天的利润,然后把正值加起来就是能获得的最大利润
        int[] profit = new int[prices.length - 1];
        int sum = 0;

        for (int i = 1; i < prices.length; i++) {
            profit[i - 1] = prices[i] - prices[i - 1];
            if (profit[i - 1] > 0) {
                sum += profit[i - 1];
            }
        }

        return sum;

    }
}
